//
//  HELModifyPwdVC.m
//  HandheldELearn
//
//  Created by 陈武琦 on 16/7/13.
//  Copyright © 2016年 陈武琦. All rights reserved.
//

#import "HELModifyPwdVC.h"
#import "NSString+Utilities.h"
#import "NSObject+CountDown.h"
@interface HELModifyPwdVC ()
@property (weak, nonatomic) IBOutlet UITextField *accoutTf;
@property (weak, nonatomic) IBOutlet UITextField *pwdNew;
@property (weak, nonatomic) IBOutlet UITextField *pwdSure;
@property (weak, nonatomic) IBOutlet UIButton *commitBtn;
@property (weak, nonatomic) IBOutlet UIButton *codeBtn;
@property (weak, nonatomic) IBOutlet UITextField *codeTf;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *codeTfTop;



@property (nonatomic,  strong) NSString *userId;


@end

@implementation HELModifyPwdVC

- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.commitBtn.layer.cornerRadius = 5;
    if (self.type == HELVcTypeRegist) {
          [self titleView:@"注册"];
          [self.commitBtn setTitle:@"注册" forState:UIControlStateNormal];
    }else if(self.type == HELVcTypeMotify){
        self.codeTfTop.constant = -1;
          [self titleView:@"修改密码"];
    }else{
         self.codeTfTop.constant = -1;
          [self titleView:@"忘记密码"];
    }
  
    self.view.backgroundColor = HexRGB_WSJ(240, 241, 246);
    // Do any additional setup after loading the view from its nib.
}
- (instancetype)init
{
    self = [super init];
    if (self) {
        self.type = HELVcTypeRegist;
    }
    return self;
}


- (BOOL)checkParams{
    
    
   
    
    if ([self.accoutTf.text isEmpty]) {
        cwqAlertMessage(@"手机号不能空");
        return NO;
    }
  
    
    if (![self.accoutTf.text isMobile]) {
        cwqAlertMessage(@"请输入正确的手机号");
        return NO;
    }
    if ([self.codeTf.text isEmpty]) {
        cwqAlertMessage(@"验证码不能为空");
        return NO;
    }
    if ([self.pwdNew.text isEmpty]) {
        cwqAlertMessage(@"请输入新密码");
        return NO;
    }
    if ([self.pwdSure.text isEmpty]) {
        cwqAlertMessage(@"请确认密码");
        return NO;
    }
    if (![self.pwdNew.text isEqualToString:self.pwdSure.text]) {
        cwqAlertMessage(@"两次密码不一致");
        return NO;
    }
    return YES;
    
}

- (IBAction)commit:(id)sender {
//    if ([self.currentPwd.text isEmpty]) {
//        alertMessage(@"请输入当前密码");
//        return;
//    }
  
    if (![self checkParams]) {
        return;
    };
  
    
    cwqHud(self.view);
    weakSelf_define;
   
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"mobile"] = self.accoutTf.text;
    params[@"code"] = self.codeTf.text;
    params[@"password"] = self.pwdNew.text;
    if (self.type == HELVcTypeRegist) {//
      
        [[HELNetworkManager sharedMg] POST:registerUrl parameters:params success:^(id responseObject) {
            [hud hide:YES];
            if (responseObject[@"exception"]) {
                cwqHudRequestError(responseObject[@"exception"]);
            }else {
                
             
                if (weakSelf.type == HELVcTypeRegist) {
                     cwqHudSuccess(@"注册成功");
                }else{
                 cwqHudSuccess(@"修改成功");
                }
               
                [weakSelf.navigationController popViewControllerAnimated:YES];
            }
            
        } failure:^(NSString *err) {
            [hud hide:YES];
            cwqHudRequestError(err);
        }];
        

    }else{
        //HELUserInfoM *userInfo = [HELAppManager userInfo];
//        if (!userInfo.userId) {
//            alertMessage(@"用户id获取失败");
//            return;
//        }
        NSString *url;
        if (self.type == HELVcTypeMotify) {
            url = helForgetPasswordUrl;
        }else{
            url = helForgetPasswordUrl;
        }
        params[@"newPwd"] = self.pwdNew.text;
        [[HELNetworkManager sharedMg] POST:url parameters:params success:^(id responseObject) {
             [hud hide:YES];
            if (responseObject[@"exception"]) {
                cwqHudRequestError(responseObject[@"exception"]);
            }else {
                
               
                cwqHudSuccess(@"修改成功");
                [weakSelf.navigationController popViewControllerAnimated:YES];
            }
            
        } failure:^(NSString *err) {
            [hud hide:YES];
            cwqHudRequestError(err);
        }];
        

    
    }
    
  }
- (IBAction)countBtnAction:(UIButton *)sender {
    
    [self sendCode];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    [self.view endEditing:YES];

}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self cancelTimer];
}
- (void)sendCode{
    if ([self.accoutTf.text isEmpty]) {
        cwqAlertMessage(@"手机号不能空");
        return ;
    }
    if (![self.accoutTf.text isMobile]) {
        cwqAlertMessage(@"请输入正确的手机号");
        return;
    }
    
    NSString *url =nil;
    if (self.type == HELVcTypeRegist) {
        url =registerCode;
    }else if(self.type == HELVcTypeForget) {
        url =helVerificationCodeUrl;
    }else if(self.type == HELVcTypeMotify){
        //修改密码  不需要验证码
        url = nil;
    }
    
    weakSelf_define;
    [[HELNetworkManager sharedMg] POST:url parameters:@{@"mobile":self.accoutTf.text} success:^(id responseObject) {
        cwqAlertMessage(@"验证码发送成功!");
        
        //倒计时
        if ([responseObject[@"root"] isEqualToString:@"success"]) {
            
            [weakSelf.codeBtn countDownTime:60 countDownBlock:^(NSUInteger timer) {
                
                
                [weakSelf.codeBtn setBackgroundColor:[UIColor lightGrayColor]];
                [weakSelf.codeBtn setTitle:[NSString stringWithFormat:@"%zd 秒", timer] forState:UIControlStateNormal];
                weakSelf.codeBtn.enabled = NO;
            } outTimeBlock:^{
                [weakSelf.codeBtn setBackgroundColor:HexRGB_WSJ(249, 55, 70)];
                [weakSelf.codeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                [weakSelf.codeBtn setTitle:@"验证码" forState:UIControlStateNormal];
                weakSelf.codeBtn.enabled = YES;
                
            }];

        }else {
            cwqHudRequestError(responseObject[@"exception"]);
        }
        self.codeBtn.enabled = YES;
    } failure:^(NSString *err) {
        self.codeBtn.enabled = YES;
        cwqHudRequestError(err);
        
    }];
    

}
- (void)dealloc{

}
@end
